package com.cloud.pole.shop.controller.shops;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.shop.model.entity.shops.ShopShopsView;
import com.cloud.pole.shop.model.vo.shops.MyShopVo;
import com.cloud.pole.shop.service.shops.ShopShopsService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/*
*2019年6月6日
*cloud-shop
*author:frank-chen
*/
@Controller
@Slf4j
@RequestMapping("/shopsCenter/front")
public class ShopsCenterController {

	@Autowired 
	private ShopShopsService shopShopsService;
	
	@ResponseBody
	@RequestMapping("/getMyShopId")
    public ApiRestResponse<Integer> getMyShopId(
    		@RequestHeader(defaultValue = "") String token){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shopShopsService.getMyShopId(token));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getMyShopInfo")
    public ApiRestResponse<MyShopVo> getMyShopInfo(
    		@RequestHeader(defaultValue = "") String token){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shopShopsService.getMyShopInfo(token));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/modifyMyShop")
    public ApiRestResponse<Boolean> modifyMyShop(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue="") String shopId,
    		@RequestParam(defaultValue="") String banner,
    		@RequestParam(defaultValue="") String shopName,
    		@RequestParam(defaultValue="") String shopOwnerName,
    		@RequestParam(defaultValue="") String longitude,
    		@RequestParam(defaultValue="") String latitude,
    		@RequestParam(defaultValue="") String provinceName,
    		@RequestParam(defaultValue="") String cityName,
    		@RequestParam(defaultValue="") String districtName,
    		@RequestParam(defaultValue="") String address,
    		@RequestParam(defaultValue="") String images){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    
			ParamException.err(StringUtils.isBlank(shopId), "shopId不能为空");    
			ParamException.err(!RegexUtils.isPositiveInteger(shopId), "shopId必须数字类型");
			ParamException.err(StringUtils.isBlank(banner), "banner不能为空");    
			ParamException.err(StringUtils.isBlank(shopName), "shopName不能为空");    
			ParamException.err(StringUtils.isBlank(shopOwnerName), "shopOwnerName不能为空");    
			ParamException.err(StringUtils.isBlank(longitude), "longitude不能为空");    
			ParamException.err(StringUtils.isBlank(latitude), "latitude不能为空");    
			ParamException.err(StringUtils.isBlank(images), "images不能为空");    
			ParamException.err(!RegexUtils.isNumberString(longitude), "longitude必须数字类型");
			ParamException.err(!RegexUtils.isNumberString(latitude), "latitude必须数字类型");

			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, shopShopsService.modifyMyShop(token, Integer.parseInt(shopId), banner, shopName, shopOwnerName, longitude, latitude, images,provinceName,cityName,districtName,address));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getMyShopViewList")
    public ApiRestResponse<ShopShopsView> getMyShopViewList(
    		@RequestHeader(name="token", defaultValue = "") String token,
    		@RequestParam(name="shopId", defaultValue="") String shopId,
    		@RequestParam(name="pageSize", defaultValue="") String pageSize,
    		@RequestParam(name="pageNum", defaultValue="") String pageNum){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");  
			ParamException.err(StringUtils.isBlank(shopId), "shopId不能为空"); 
			ParamException.err(!RegexUtils.isPositiveInteger(shopId), "shopId错误");
			ParamException.err(StringUtils.isBlank(pageSize), "pageSize不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(pageSize), "pageSize错误");
			ParamException.err(StringUtils.isBlank(pageNum), "pageNum错误");
			ParamException.err(!RegexUtils.isPositiveInteger(pageNum), "pageNum错误");
			
			PageInfo<ShopShopsView> pageInfo =  shopShopsService.getMyShopViewList(token, Integer.parseInt(shopId), Integer.parseInt(pageSize), Integer.parseInt(pageNum));
			return ApiRestResponse.successOfPage(CommonConstants.STATUS_OK_MESSAGE, pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getList());
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
